<template>
  <view>
    <my-search @click.native="goSearch()"></my-search>
    <view class="scroll-view">
      <scroll-view class="scroll-left" scroll-y="true" :style="{height:wh+'px'}">
        <view @click="changeActive(index)" :class="['scroll-left-item' ,index === active ? 'active' : '']"
          v-for="(item,index) in cateList" :key="index">{{item.cat_name}}</view>
      </scroll-view>
      <scroll-view class="scroll-right" :scroll-top="scrollHeight" scroll-y="true" :style="{height:wh+'px'}">
        <view class="scroll-right-item" v-for="(item,index) in cateLvel2" :key="index">
          <view class="title">
            /{{item.cat_name}}/
          </view>
          <view class="text">
            <view @click="cateClickHandler(item2.cat_id)" class="item" v-for="(item2,index2) in item.children"
              :key="index2">
              <image mode="widthFix" :src="item2.cat_icon"></image>
              <view style="font-size: 12px;margin-left: 10rpx;margin-bottom: 20rpx;">
                {{item2.cat_name}}
              </view>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import tabbarMixins from '@/mixins/tabbar.js'
  export default {
    mixins: [tabbarMixins],
    data() {
      return {
        wh: 0,
        active: 0,
        cateList: [],
        cateLvel2: [],
        scrollHeight: 0
      }
    },
    methods: {
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.cateList = res.message
        this.cateLvel2 = res.message[0].children
      },
      changeActive(index) {
        this.active = index
        this.cateLvel2 = this.cateList[index].children
        this.scrollHeight = this.scrollHeight === 0 ? 1 : 0
      },
      cateClickHandler(id) {
        uni.navigateTo({
          url: "/subpkg/goods-list/goods-list?cid=" + id
        })
      },
      goSearch() {
        uni.navigateTo({
          url: "/subpkg/goods-search/goods-search"
        })
      }
    },
    onLoad() {
      const sysInfo = uni.getSystemInfoSync()
      this.wh = sysInfo.windowHeight - 50
      this.getCateList()
    },
  }
</script>

<style lang="scss">
  .scroll-view {
    display: flex;

    .scroll-left {
      flex: 1;
      background-color: seashell;

      .scroll-left-item {
        height: 100rpx;
        text-align: center;
        line-height: 100rpx;
        font-size: 12px;

        &.active {
          background-color: white;
          position: relative;

          &::before {
            content: ' ';
            display: block;
            width: 6rpx;
            height: 60rpx;
            background-color: #c00000;
            position: absolute;
            top: 20%;
            left: 0;
          }
        }
      }
    }

    .scroll-right {
      flex: 3;

      .scroll-right-item {
        background-color: white;
        padding: 30rpx 0rpx;

        .title {
          margin: 30rpx 0;
          text-align: center;
          font-size: 14px;
          font-weight: 900;
        }

        .text {
          display: flex;
          flex-wrap: wrap;
          margin-left: 30rpx;

          .item {
            flex: 30%;
            width: 30%;

            image {
              width: 120rpx;
            }
          }
        }
      }
    }
  }
</style>